package cn.hzw.doodle;

import static cn.hzw.doodle.util.DrawUtil.rotatePoint;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;

import cn.hzw.doodle.core.IDoodle;
import cn.hzw.doodle.util.DoodleConstants;

/**
 * 可旋转的item
 */
public abstract class DoodleRotatableItemBase extends DoodleSelectableItemBase {

    private final PointF mTemp = new PointF();
    private final Rect mRectTemp = new Rect();
    private boolean mIsRotating = false;
    private final Paint mPaint = new Paint();

    public DoodleRotatableItemBase(IDoodle doodle, int itemRotate, float x, float y) {
        super(doodle, itemRotate, x, y);
    }

    public DoodleRotatableItemBase(IDoodle doodle, DoodlePaintAttrs attrs, int itemRotate, float x, float y) {
        super(doodle, attrs, itemRotate, x, y);
    }

    @Override
    public void doDrawAtTheTop(Canvas canvas) {
        if (isSelected()) {
            // 反向缩放画布，使视觉上选中边框不随图片缩放而变化
            int count = canvas.save();
//            canvas.scale(1 / getDoodle().getDoodleScale(), 1 / getDoodle().getDoodleScale(), getPivotX() - getLocation().x, getPivotY() - getLocation().y);
            mRectTemp.set(getBounds());
//            DrawUtil.scaleRect(mRectTemp, getDoodle().getDoodleScale(), getPivotX() - getLocation().x, getPivotY() - getLocation().y);

            float unit = getDoodle().getUnitSize();
            float scale = getDoodle().getDoodleScale();
            float padding = DoodleConstants.ITEM_PADDING * unit;
            mRectTemp.left -= padding;
            mRectTemp.top -= padding;
            mRectTemp.right += padding;
            mRectTemp.bottom += padding;
            mPaint.setShader(null);
            mPaint.setColor(DoodleConstants.ITEM_COLOR_DEFAULT_RECT);
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setStrokeWidth(1);
            canvas.drawRect(mRectTemp, mPaint);

            // border
            if (isRotating()) {
                mPaint.setColor(DoodleConstants.ITEM_COLOR_SELECT);
            } else {
                mPaint.setColor(DoodleConstants.ITEM_COLOR_DEFAULT);
            }
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeWidth(2 * unit);
            canvas.drawRect(mRectTemp, mPaint);
            // border line
            mPaint.setColor(DoodleConstants.ITEM_COLOR_DEFAULT_LINE);
            mPaint.setStrokeWidth(0.8f * unit);
            canvas.drawRect(mRectTemp, mPaint);

            mPaint.setColor(Color.WHITE);
            mPaint.setStyle(Paint.Style.FILL);
            int size = (int) (DoodleConstants.ITEM_ICON_SIZE * unit / scale);
            float halfSize = size / 2f;
            canvas.drawCircle(mRectTemp.left, mRectTemp.top, halfSize, mPaint);
            canvas.drawCircle(mRectTemp.right, mRectTemp.bottom, halfSize, mPaint);
            mPaint.setColor(DoodleConstants.ITEM_COLOR_DEFAULT_LINE);
            mPaint.setStyle(Paint.Style.STROKE);
            canvas.drawCircle(mRectTemp.left, mRectTemp.top, halfSize, mPaint);
            canvas.drawCircle(mRectTemp.right, mRectTemp.bottom, halfSize, mPaint);
            mPaint.setColor(Color.BLACK);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStrokeWidth(2f * unit / scale);
            float margin = halfSize / 2f;
            canvas.drawLine(mRectTemp.left - margin, mRectTemp.top - margin,
                    mRectTemp.left + margin, mRectTemp.top + margin, mPaint);
            canvas.drawLine(mRectTemp.left - margin, mRectTemp.top + margin,
                    mRectTemp.left + margin, mRectTemp.top - margin, mPaint);

            canvas.drawArc(mRectTemp.right - margin, mRectTemp.bottom - margin,
                    mRectTemp.right + margin, mRectTemp.bottom + margin,
                    90, 270, false, mPaint);

            canvas.restoreToCount(count);
        }
    }

    /**
     * 是否可以删除
     */
    public boolean canDelete(float x, float y) {
        x = x - getLocation().x;
        y = y - getLocation().y;
        PointF xy = rotatePoint(mTemp, (int) -getItemRotate(), x, y,
                getPivotX() - getLocation().x, getPivotY() - getLocation().y);
        mRectTemp.set(getBounds());
        float unit = getDoodle().getUnitSize();
        float padding = DoodleConstants.ITEM_PADDING * unit;
        mRectTemp.top -= padding;
        mRectTemp.left -= padding;
        mRectTemp.right += padding;
        mRectTemp.bottom += padding;
        int size = (int) (DoodleConstants.ITEM_ICON_SIZE * unit / getDoodle().getDoodleScale());
        float halfSize = size / 2f;
        return xy.x >= mRectTemp.left - halfSize
                && xy.x <= mRectTemp.left + halfSize
                && xy.y >= mRectTemp.top - halfSize
                && xy.y <= mRectTemp.top + halfSize;
    }

    /**
     * 是否可以旋转
     */
    public boolean canRotate(float x, float y) {
        // 把触摸点转换成在item坐标系（即以item起始点作为坐标原点）内的点
        x = x - getLocation().x;
        y = y - getLocation().y;
        // 把变换后矩形中的触摸点，还原回未变换前矩形中的点，然后判断是否矩形中
        PointF xy = rotatePoint(mTemp, (int) -getItemRotate(), x, y,
                getPivotX() - getLocation().x, getPivotY() - getLocation().y);
        mRectTemp.set(getBounds());
        float unit = getDoodle().getUnitSize();
        float padding = DoodleConstants.ITEM_PADDING * unit;
        mRectTemp.top -= padding;
        mRectTemp.left -= padding;
        mRectTemp.right += padding;
        mRectTemp.bottom += padding;
        int size = (int) (DoodleConstants.ITEM_ICON_SIZE * unit / getDoodle().getDoodleScale());
        float halfSize = size / 2f;
        return xy.x >= mRectTemp.right - halfSize
                && xy.x <= mRectTemp.right + halfSize
                && xy.y >= mRectTemp.bottom - halfSize
                && xy.y <= mRectTemp.bottom + halfSize;
    }

    public boolean isRotating() {
        return mIsRotating;
    }

    public void setIsRotating(boolean isRotating) {
        mIsRotating = isRotating;
    }
}
